package org.bitmap.mm.util;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.support.v4.util.LruCache;

/**
 * 图片缓存管理器
 * @author hljdrl@gmail.com
 *
 */
public class ImageCaches {

	private static final int DEFAULT_MEM_CACHE_SIZE = 1024 * 1024 * 2; // 4MB

	private LruCache<String, Bitmap> mMemoryCache;
	public HashMap<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();
	private Context mContext;

	public ImageCaches(Context context) {
		mContext = context;
		mMemoryCache = new LruCache<String, Bitmap>(DEFAULT_MEM_CACHE_SIZE) {

			@Override
			protected int sizeOf(String key, Bitmap value) {
				return getBitmapSize(value);
			}

			@Override
			protected void entryRemoved(boolean evicted, String key,
					Bitmap oldValue, Bitmap newValue) {
				if (oldValue != null) {
					imageCache.put(key, new SoftReference<Bitmap>(oldValue));
					System.gc();
				}
			}
		};

	}

	public static int getBitmapSize(Bitmap value) {
		if (value != null) {
			if (value != null) {
				return value.getRowBytes() * value.getHeight();
			}
		}
		return 0;
	}
	boolean hasImageAsLocationFile(String data){
		final String key = FileLove.hashKeyForDisk(data);
		File imageFile = new File(FileLove.getExternalCacheDir(mContext), key);
		if(imageFile.exists())
			return true;
		
		return false;
	}
	File getImageAsLocationFile(String data){
		final String key = FileLove.hashKeyForDisk(data);
		File imageFile = new File(FileLove.getExternalCacheDir(mContext), key);
		return imageFile;
	}
	
	/**
	 * 加载本地已经缓存的图片
	 * @param data
	 * @param reqWidth
	 * @param config
	 * @return
	 */
	public Bitmap getBitmapFromDiskCache(String data, int reqWidth,
			Bitmap.Config config) {
		final String key = FileLove.hashKeyForDisk(data);
		File imageFile = new File(FileLove.getExternalCacheDir(mContext), key);
		Bitmap bm = null;
		bm = getBitmapFromMemCache(data, reqWidth, config);
		if (bm == null && imageFile.exists()) {
			if (reqWidth==-100){
				bm = BitmapFactory.decodeFile(imageFile.toString(),
						Config.getMaxImageOptions(config));
			}else 
			if (reqWidth > 0) {
				bm = BitmapLove.decodeSampledBitmapFromFile(
						imageFile.toString(), reqWidth, config);
			} else {
				bm = BitmapFactory.decodeFile(imageFile.toString(),
						Config.getIconOptions(1, config));
			}
		}
		return bm;
	}

	 synchronized void addBitmapToCache(String data, Bitmap bitmap) {
		if (data == null || bitmap == null) {
			return;
		}
		String key = FileLove.hashKeyForDisk(data);
		// Add to memory cache
		if (mMemoryCache != null && mMemoryCache.get(key) == null) {
			if (bitmap != null) {
				mMemoryCache.put(key, bitmap);
			}
		}
	}

	/**
	 *  获得本地已经存在的bitmap缓存，是已经加载到内存中的，就不许要在加载文件在加入到内存中。
	 * @param data
	 * @param reqWidth
	 * @param cofnig
	 * @return
	 */
	public Bitmap getBitmapFromMemCache(String data, int reqWidth,
			Bitmap.Config cofnig) {
		Bitmap memBitmap = null;
		String key = FileLove.hashKeyForDisk(data);
		if (mMemoryCache != null) {
			Bitmap soft = mMemoryCache.get(key);
			if (soft != null) {
				memBitmap = soft;
			}
			if (memBitmap != null) {
				return memBitmap;
			} else {
				File cache = new File(FileLove.getExternalCacheDir(mContext),
						key);
				if (cache.exists()) {
					Bitmap bm = null;
					if (reqWidth==-100){
						bm = BitmapFactory.decodeFile(cache.toString(),
								Config.getMaxImageOptions(cofnig));
					}
					else if (reqWidth > 0) {
						bm = BitmapLove.decodeSampledBitmapFromFile(
								cache.toString(), reqWidth, cofnig);
					} else {
						bm = BitmapFactory.decodeFile(cache.toString(),
								Config.getIconOptions(2, cofnig));
					}
					if (bm != null) {
						addBitmapToCache(data, bm);
					}
					return bm;
				}

			}
		}
		return memBitmap;
	}

	 Bitmap getBitmapFromDiskCacheIcon(String data, Bitmap.Config config) {
		 if(data!=null&&data.length()>0){
		final String key = FileLove.hashKeyForDisk(data);
		File imageFile = new File(FileLove.getExternalCacheDir(mContext), key);
		Bitmap bm = null;
		bm = getBitmapFromMemCacheAsIcon(data, config);
		if (bm == null && imageFile.exists()) {
			Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = config;
			bm = BitmapFactory.decodeFile(imageFile.toString(), opt);
		}
		return bm;
		 }
		 return null;
	}

	 Bitmap getBitmapFromMemCacheAsIcon(String data, Bitmap.Config cofnig) {
		Bitmap memBitmap = null;
		String key = FileLove.hashKeyForDisk(data);
		if (mMemoryCache != null) {
			Bitmap soft = mMemoryCache.get(key);
			if (soft != null) {
				memBitmap = soft;
			}
			return memBitmap;
		}
		return memBitmap;
	}

	public void clearCaches() {
		mMemoryCache.evictAll();
	}
}
